<template>
  <section>
    <el-card class="search-card">
      <BasicTableForm ref="formData"
        isScalingBtn
        :colNumOfRow="4"
        formItemWidth="120px"
        :formColumns="formItemList"
        :dataSource="formData"
        :defaultExpandStatus="false"
        :handleSearchForm="handleQueryForm" />
    </el-card>
    <el-card class="table-card">
      <basic-table
        showActionCol
        :showSelectCol="false"
        :loading="tableInfo.loading"
        :columns="tableInfo.columns"
        :dataSource="tableInfo.list"
        :pagination="pagination"
        :tableHeight="460"
        :actionColWidth="160"
        :actionRender="renderTableBtnAction"
        @pageChange="handlePageChange"
        @pageSizeChange="handlePageSizeChange"
      >
        <div slot="content_context">
          <el-button
            v-if="showBtn('导出') && supplierExportConfig.supplier"
            :disabled="!pagination.total"
            size="mini"
            type="primary"
            icon="el-icon-download"
            :loading="exporting"
            @click="handleExportFile"
            >导出</el-button>
          <el-button
            v-if="showBtn('新增')"
            size="mini"
            type="primary"
            @click="handleCreateSupplier"
            >新建特殊供应商</el-button>
        </div>
      </basic-table>
      <!-- <AdvancedTable
        :pagination="pagination"
        :dataSource="tableInfo.list"
        :columns="tableInfo.columns"
        :loading="tableInfo.loading"
        :toolbarConfig="toolbarConfig"
        @pageChange="handlePageChange"
        @pageSizeChange="handlePageSizeChange">
        <template #toolbar_buttons>
          <el-button type="primary" @click="handleExportFile">导出</el-button>
          <el-button type="primary" @click="handleCreateSupplier">新建特殊供应商</el-button>
        </template>
        <template #operate="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleEliminateSuppliers(row)">淘汰供应商</el-button>
        </template>
      </AdvancedTable> -->
    </el-card>
    <ConvertSupplierDialog
      :convertType="convertType"
      :supplier="supplier"
      :dialogVisible="dialogVisible"
      @handleClose="handleDialogClose" />
  </section>
</template>

<script>
import { mapGetters } from 'vuex';
import { getBtnsId } from '@/libs/btnsPermission';
import BasicTableForm from '@/components/supplier/BasicForm'; // 表单组件
import BasicTable from '@/components/supplier/BasicTable';
// import AdvancedTable from '@/components/supplier/AdvancedTable'; // 表格组件
// import BaseList from '@/mixins/supplier/baseList'; // 表格Mixin
import ConvertSupplierDialog from '@/pages/supplier/components/convertSupplierDialog'; // 转化弹窗
import {
  querySpecialSupplierListService,
  exportSpecialSupplierListService
} from '@/api/supplier/management/specialSupplier';
import { createExcelByExport, handleOpenNewPage } from '@/utils/bizUtils';
import { getHasIndexTableData } from '@/pages/supplier/utils/bizUtils';
import BasicPage from '@/mixins/supplier/basicPage';
import Dict from '@/mixins/supplier/dict';
import ManageArea from '@/mixins/supplier/manageArea';
import { INITIAL_FIELDS_VALUE, FORM_ITEM_LIST, TABLE_COLUMNS } from './constants';

export default {
  mixins: [Dict, ManageArea, BasicPage],
  components: {
    BasicTableForm,
    // AdvancedTable,
    BasicTable,
    ConvertSupplierDialog,
  },
  computed: {
    ...mapGetters('permissions/systemBtns', ['showBtn']),
  },
  data () {
    return {
      formItemList: FORM_ITEM_LIST,
      tableInfo: {
        list: [],
        selection: [],
        columns: TABLE_COLUMNS,
        loading: false
      },
      pagination: {
        current: 1,
        total: 0,
        size: 10
      },
      formData: Object.assign({}, INITIAL_FIELDS_VALUE),
      // toolbarConfig: {
      //   slots: {
      //     // 自定义工具栏模板
      //     buttons: 'toolbar_buttons',
      //   }
      // },
      exporting: false,
      convertType: '10',
      dialogVisible: false,
      supplier: {},
    };
  },
  created () {
    sessionStorage.setItem('buttonId', getBtnsId('查询'));
    this.getTableData();
  },
  methods: {
    // 导出
    handleExportFile () {
      this.exporting = true
      exportSpecialSupplierListService(this.getRequestParams()).then(res => {
        this.exporting = false
        createExcelByExport({ excelTitle: '特殊供应商信息', res })
      })
    },
    handleQueryForm () {
      this.getTableData(1);
    },
    // 页码切换
    handlePageChange (val) {
      this.getTableData(val);
    },
    // 每页条数切换
    handlePageSizeChange (val) {
      this.getTableData(1, val);
    },
    // 获取列表查询参数
    getRequestParams () {
      const {
        supplierCode,
        supplierName,
        mainCategoryId,
        supplierType,
        managementAreaId,
        paidType,
        checkoutRange,
        settlementType,
        centralizedSettle,
        centralizedSettlePlace,
      } = this.formData
      return {
        supplierCode,
        supplierName,
        mainCategoryId,
        supplierType,
        managementAreaId,
        paidType,
        checkoutRange,
        settlementType,
        centralizedSettle,
        centralizedSettlePlace,
      }
    },
    async getTableData (current = this.pagination.current, size = this.pagination.size) {
      this.tableInfo.loading = true
      const params = Object.assign({ current, size }, this.getRequestParams())
      const resp = await querySpecialSupplierListService(params)
      this.tableInfo.loading = false
      if (resp && resp.result) {
        const { records, total, pages } = resp.result
        this.pagination = Object.assign(this.pagination, { current, size, total, pages })
        this.tableInfo.list = getHasIndexTableData(records, this.pagination)
      } else {
        this.tableInfo.list = []
      }
    },
    // 渲染表格操作栏按钮
    renderTableBtnAction (h, { row }) {
      return <div>
        <el-button type="text" disabled={!this.showBtn('编辑')} nativeOnClick={() => { this.handleEdit(row) }}>编辑</el-button>
        <el-button type="text" disabled={!this.showBtn('淘汰供应商')} nativeOnClick={() => { this.handleEliminateSuppliers(row) }}>淘汰供应商</el-button>
      </div>
    },
    // 新建特殊供应商
    handleCreateSupplier () {
      // this.$router.push({ name: 'addSpecialSupplier' });
      handleOpenNewPage(this, { name: 'addSpecialSupplier' });
    },
    // 编辑
    handleEdit (data) {
      this.queryDataPermissionAction(data.supplierId).then(res => {
        if (res) {
          this.$router.push({ name: 'editSpecialSupplier', query: { id: data.supplierId } });
        }
      })
    },
    // 淘汰
    handleEliminateSuppliers (data) {
      console.log(data)
      this.supplier = data
      this.dialogVisible = true
    },
    // 关闭转化弹窗
    handleDialogClose (data) {
      this.dialogVisible = false
      if (data && data.status === '0') {
        this.$message.success('操作成功！')
        this.getTableData()
      }
    },
  }
}
</script>

<style lang="less" scope>

</style>
